<?php

	/**
	 * @package		CoreProject
	 * @subpackage	Core
	 *
	 * cpr_Core_Utils
	 * 
	 */

	class cpr_Core_Utils
	{
		/**
		 * Закрытый конструктор класса
		 */
		private function __construct() {}

		/**
		 * @param	string	$_string
		 * @return	string
		 */
		public static function ShowCrLf($_string)
		{
			return str_replace("\n", '\n', str_replace("\r", '\r', $_string));
		}

		/**
		 * @param	string	$_str
		 * @return	string
		 */
		public static function ClearRequest($_str)
		{
			if (@get_magic_quotes_gpc())
			{
				return stripslashes($_str);
			}
			return $_str;
		}

		/**
		 * @param	string	$_string
		 * @return	string
		 */
		public static function ClearNRT($_string)
		{
			return str_replace(array("\n", "\r", "\t"), '', $_string);
		}

		/**
		 * @param	string	$_attributeValue
		 * @return	string
		 */
		public static function AttributeQuote($_attributeValue)
		{
			return str_replace('\'', '&#039;', str_replace('"', '&quot;', $_attributeValue));
		}
		
		/**
		 * @param	string	$_path
		 * @return	string
		 */
		public static function PathPreparation($_path)
		{
			return rtrim(str_replace('\\', '/', trim($_path)), '/').'/';
		}
		
		/**
		 * @param	string	$_fileName
		 * @return	string
		 */
		public static function FileNamePreparation($_fileName)
		{
			return str_replace('\\', '/', trim($_fileName));
		}
		
		/**
		 * @param	string	$_html
		 * @return	string
		 */
		public static function PackHtml($_html)
		{
			return preg_replace('/[\s]+/', ' ', trim($_html));
		}
		
		/**
		 * @param	int		$_sizeInByte
		 * @return	string
		 */
		public static function FrienlySize($_sizeInByte)
		{
			if ($_sizeInByte >= 1048576)
			{
				$_sizeInByte = round($_sizeInByte / 1048576 * 100) / 100 . 'MB';
			}
			else if ($_sizeInByte >= 1024)
			{
				$_sizeInByte = round($_sizeInByte / 1024 * 100) / 100 . 'KB';
			}
			else
			{
				$_sizeInByte = $_sizeInByte . 'B';
			}
			return $_sizeInByte;
		}

		/**
		 * @param	string	$_phpText
		 * @return	string
		 */
		public static function ClearPhpFile($_phpText)
		{
			$_phpText = trim($_phpText);
			if (substr($_phpText, 0, 5) === '<?php')
			{
				$_phpText = substr($_phpText, 5);
			}
			else if (substr($_phpText, 0, 2) === '<?')
			{
				$_phpText = substr($_phpText, 2);
			}

			if (substr($_phpText, -2) === '?>')
			{
				$_phpText = substr($_phpText, 0, -2);
			}

			$_phpText = preg_replace('/\/\*.*?\*\//s', ' ', $_phpText);
			$_phpText = preg_replace('/[\s]+/', ' ', $_phpText);

			return $_phpText;
		}

		/**
		 * @param	string	$password
		 * @return	string
		 */
		static public function ClearVersion($version)
		{
			return preg_replace('/[^0-9]/', '', $version);
		}

		/**
		 * @param	string	$value
		 * @return	bool
		 */
		static public function IsLatin($value)
		{
			for($i = 0,  $c = strlen($value); $i < $c; $i++)
			{
				$ch = $value{$i};
				$ord = ord($ch);
				if (($ord >= 0x80) || ($ord < 0x20))
				{
					if ($ord != 13 && $ord != 10 && $ord != 9)
					{
						return false;
					}
				}
			}
			return true;
		}

		static public function ConvertEncoding($str, $fromCharset, $toCharset)
		{
			if (strtolower($fromCharset) == strtolower($toCharset))
			{
				return $str;
			}

			$result = @iconv($fromCharset, $toCharset.'//IGNORE', $str);
			return ($result !== false) ? $result : $str;
		}

		/**
		 * @param	string	$utf8str
		 * @param	int		$strlen
		 * @return	array
		 */
		static public function Utf8ChunkSplit($utf8str, $strlen)
		{
			$start = 0;
			$textlen = $strlen;
			$out = array();
			while (true)
			{
				$Offset = 6;
				$Kod = @ord($utf8str{$start + $textlen}) >> $Offset;
				while ($Kod == 2)
				{
					$textlen--;
					$Kod = @ord($utf8str{$start + $textlen}) >> $Offset;
				}
				$temp = substr($utf8str,$start,$textlen);

				if (!$temp && $temp !== '0')
				{
					break;
				}
				$out[] = $temp;
				$start += $textlen;
			}
			return $out;
		}
	}
